let e = document.getElementById("qiban")

let canvas = e.getContext("2d")
canvas.strokeStyle = "#BFBFBF"
let cellWidth = (450 - 30) / 14

let createChessBox = function() {
	for (let i = 0; i < 15; i++) {
		canvas.moveTo(15, 15 + i * cellWidth)
		canvas.lineTo(450 - 15, 15 + i * cellWidth)
		canvas.stroke()
		canvas.moveTo(15 + i * cellWidth, 15)
		canvas.lineTo(15 + i * cellWidth, 450 - 15)
		canvas.stroke()
	}
}

let iconBg = new Image()
iconBg.src = "images/icon_bg.png"
iconBg.onload = function() {
	canvas.drawImage(iconBg, 0, 0, 450, 450)
	createChessBox()
}

let over = false;
let me = true;
let array = [];
for (let i = 0; i < 15; i++) {
	array[i]=[]
	for (let j = 0; j < 15; j++) {
		array[i][j] = 0;
	}
}

let wins = []
let myWin = []
let computerWin = []
for (let i = 0; i < 15; i++) {
	wins[i] = [];
	for (let j = 0; j < 15; j++) {
		wins[i][j] = [];
	}
}
let count = 0
for (let i = 0; i < 15; i++) { //横方向
	for (let j = 0; j < 11; j++) {
		for (let k = 0; k < 5; k++) {
			wins[i][j + k][count] = true
		}
		count++;
	}
}
for (let i = 0; i < 11; i++) { //竖方向
	for (let j = 0; j < 15; j++) {
		for (let k = 0; k < 5; k++) {
			wins[i + k][j][count] = true
		}
		count++;
	}
}
for (let i = 0; i < 11; i++) { //正斜线
	for (let j = 0; j < 11; j++) {
		for (let k = 0; k < 5; k++) {
			wins[i + k][j + k][count] = true
		}
		count++;
	}
}
for (let i = 4; i < 15; i++) { //
	for (let j = 0; j < 11; j++) {
		for (let k = 0; k < 5; k++) {
			wins[i - k][j + k][count] = true
		}
		count++;
	}
}
console.log("count==" + count)
for (let i = 0; i < count; i++) {
	myWin[i] = 0
	computerWin = 0
}
let onStep = function(x, y, me) {
	if (over) {
		return;
	}
	canvas.beginPath()
	canvas.arc(15 + x * cellWidth, 15 + y * cellWidth, 13, 0, 2 * Math.PI)
	canvas.closePath()
	let gradient = canvas.createRadialGradient(15 + x * cellWidth, 15 + y * cellWidth, 0, 15 + x * cellWidth, 15 +
		y * cellWidth, 13)
	if (me) {
		gradient.addColorStop(0, "#0a0a0a")
		gradient.addColorStop(1, "#636766")
	} else {
		gradient.addColorStop(1, "#d1d1d1")
		gradient.addColorStop(0, "#f9f9f9")
	}
	canvas.fillStyle = gradient
	canvas.fill()
}

e.onclick = function(e) {
	if (me) {
		let x = e.offsetX
		let y = e.offsetY
		let i = Math.floor((x - (15 - cellWidth / 2)) / cellWidth)
		let j = Math.floor((y - (15 - cellWidth / 2)) / cellWidth)
		console.log("x====" + x)
		if (j < 15 && i < 15) {
			console.log("点击了u===="+i+";===v==="+j+";position==="+(array[i][j]))
			if (array[i][j] == 0) {
				console.log("我下了u===="+i+";===v==="+j)
				array[i][j] = "1"
				onStep(i, j, true)
				for (let c = 0; c < count; c++) {
					if (wins[i][j][c]) {
						myWin[c]++
						computerWin[c] = 6
						if (myWin[c] == 5) {
							over = true;
							setTimeout(() => {
								alert("我赢了")
							}, 100)
						}
					}
				}
				me = !me;
				if (!over) {
					computerSetp()
				}
			}
		}
	}
}

let computerSetp = function() {
	let myScore = [];
	let computerScore = [];
	let u = 0
	let v = 0
	let max = 0
	for (let i = 0; i < 15; i++) {
		myScore[i] = []
		computerScore[i] = []
		for (let j = 0; j < 15; j++) {
			myScore[i][j] = 0
			computerScore[i][j] = 0
		}
	}

	for (let i = 0; i < 15; i++) {
		for (let j = 0; j < 15; j++) {
			if (array[i][j] == 0) {
				let c = count;
				for (let count = 0; count < c; count++) {
					if (wins[i][j][count]) {
						if(myWin[count] == 0){
							myScore[i][j] += 100;
						}else if (myWin[count] == 1) {
							myScore[i][j] += 200;
						} else if (myWin[count] == 2) {
							myScore[i][j] += 400;
						} else if (myWin[count] == 3) {
							myScore[i][j] += 1000;
						} else if (myWin[count] == 4) {
							myScore[i][j] += 10000;
						}

						if(computerWin[count] == 0){
							computerWin[i][j] += 110;
						}else if (computerWin[count] == 1) {
							computerScore[i][j] += 210;
						} else if (myWin[count] == 2) {
							computerScore[i][j] += 410;
						} else if (myWin[count] == 3) {
							computerScore[i][j] += 1100;
						} else if (myWin[count] == 4) {
							computerScore[i][j] += 20000;
						}
					}
				}
			}
		}
	}

	for (let i = 0; i < 15; i++) {
		for (let j = 0; j < 15; j++) {
			if (myScore[i][j] > max) {
				max = myScore[i][j]
				u = i;
				v = j;
			} else if (myScore[i][j] == max) {
				if (computerScore[i][j] > computerScore[u][v]) {
					u = i;
					v = j;
				}
			}

			if (computerScore[i][j] > max) {
				max = computerScore[i][j]
				u = i;
				v = j;
			} else if (computerScore[i][j] == max) {
				if (myScore[i][j] > myScore[u][v]) {
					u = i;
					v = j;
				}
			}
		}
	}

	array[u][v] = "2"
	onStep(u, v, false)
	me = !me
	console.log("电脑下了u===="+u+";===v==="+v)
	for (let c = 0; c < count; c++) {
		if (wins[u][v][c]) {

			computerWin[c]++
			myWin[c] = 6
			if (computerWin[c] == 5) {
				over = true;
				setTimeout(() => {
					alert("电脑赢了")
				}, 100)
			}
		}
	}
}
